package com.ljx.recruitment.interview.service.impl.process;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.ljx.recruitment.common.exception.ApiException;
import com.ljx.recruitment.interview.constant.JobConstant;
import com.ljx.recruitment.interview.domain.entity.Deliver;
import com.ljx.recruitment.interview.domain.vo.req.DeliverSubmitReq;
import com.ljx.recruitment.interview.enums.DeliverOptionEnum;
import com.ljx.recruitment.interview.enums.DeliverStatusEnum;
import com.ljx.recruitment.interview.enums.JobBaseProcessEnum;
import com.ljx.recruitment.interview.service.DeliverProcess;
import com.ljx.recruitment.interview.service.DeliverService;
import com.ljx.recruitment.interview.typehandler.details.DeliverDetails;
import com.ljx.recruitment.mq.dto.DeliverEmailObjDto;
import com.ljx.recruitment.mq.dto.MqTransferDto;
import com.ljx.recruitment.mq.enums.MQTypeEnum;
import com.ljx.recruitment.mq.producer.RabbitMQProducer;
import com.ljx.recruitment.user.enums.EmailTypeEnum;
import com.ljx.recruitment.user.util.EmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;


/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-11-04  15:51
 */
@Service
public class ResumeScreeningProcess implements DeliverProcess {

    @Autowired
    private DeliverService deliverService;

    @Autowired
    private RabbitMQProducer rabbitMQProducer;

    @Override
    public String getTitle(Deliver deliver) {
        return DeliverStatusEnum.RESUME_SCREENING.getTitle() + "_" + deliver.getBatch();
    }

    @Override
    public void handleDeliver(Deliver deliver, DeliverSubmitReq req, Integer option) {
        if (DeliverOptionEnum.PASS.getKey().equals(option)) {
            checkParams(deliver.getDeliverStatus(), req.getExtObject());
        }
        handler(deliver, req);
        afterHandler(deliver, req, option);
    }

    private void handler(Deliver deliver, DeliverSubmitReq req) {
        // 更新投递参数
        DeliverDetails deliverDetails = deliver.getDeliverDetails();
        Map<String, String> paramsMap = req.getExtObject();
        deliverDetails.put(getTitle(deliver), paramsMap);
        deliver.setDeliverDetails(deliverDetails);
        deliverService.updateById(deliver);
        // 设置标题参数（因为不需要保存在投递数据中，就存在在paramsMap）
        paramsMap.put(JobConstant.EMAIL_INTERVIEW_TITLE_DETAILS, JSONUtil.toJsonStr(req.getTitleDetails()));
        paramsMap.put(JobConstant.NEXT_DELIVER_STATUS, deliver.getDeliverStatus().toString());
        req.setExtObject(paramsMap);
    }

    private void checkParams(Integer deliverStatus, Map<String, String> details) {
        if (JobBaseProcessEnum.RESUME.getKey().equals(deliverStatus)) {

        } else if (JobBaseProcessEnum.WRITTEN_EXAMINATION.getKey().equals(deliverStatus)) {
            if (StrUtil.isEmpty(details.get("url")) || Objects.isNull(details.get("beginTime")) ||
                    Objects.isNull(details.get("endTime"))){
                throw new ApiException("必要参数不能为空");
            }
        } else if (JobBaseProcessEnum.MAJOR_INTERVIEW.getKey().equals(deliverStatus)) {
            if (StrUtil.isEmpty(details.get("location")) || Objects.isNull(details.get("beginTime")) ||
                    Objects.isNull(details.get("endTime")) || Objects.isNull(details.get("method"))){
                throw new ApiException("必要参数不能为空");
            }
        } else if (JobBaseProcessEnum.HR_INTERVIEW.getKey().equals(deliverStatus)) {
            if (StrUtil.isEmpty(details.get("location")) || Objects.isNull(details.get("beginTime")) ||
                    Objects.isNull(details.get("endTime")) || Objects.isNull(details.get("method"))){
                throw new ApiException("必要参数不能为空");
            }
        } else if (JobBaseProcessEnum.OFFER.getKey().equals(deliverStatus)) {

        } else {
            throw new ApiException("非法操作");
        }
    }

    private void afterHandler(Deliver deliver, DeliverSubmitReq req, Integer option) {
        rabbitMQProducer.sendMQMsg(new MqTransferDto(MQTypeEnum.EMAIL.getKey(),
                new DeliverEmailObjDto(EmailTypeEnum.RESUME_SCREENING.getKey(), option,
                        deliver.getId(), req.getExtObject())));
    }

}
